import {
  createRouter,
  createWebHistory,
  // createWebHashHistory,
  type RouteRecordRaw,
} from "vue-router";

const routes: RouteRecordRaw[] = [
  {
    path: "/",
    name: "WebForWeChat",
    redirect: '/home'
  },
  {
    name: "home",
    path: "/home",
    component: () => import("@/views/Home.vue"),
    // redirect: '/',
    children: [
    ],
    beforeEnter: (to, from, next) => {
      let phone = localStorage.getItem('phone')
      if (phone === '13817763538') next()
      else next('/login')
    }
  },
  {
    name: "iframe",
    path: "/iframe",
    component: () => import("@/views/iframe.vue"),
    // redirect: '/',
    children: [
    ],
    beforeEnter: (to, from, next) => {
      let phone = localStorage.getItem('phone')
      if (phone === '13817763538') next()
      else next('/login')
    }
  },
  {
    name: "login",
    path: "/login",
    component: () => import("@/views/login.vue"),
    beforeEnter: (to, from, next) => {
      let phone = localStorage.getItem('phone')
      if (phone === '13817763538') next('/home')
      else next()
    }
  }



]


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  // history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      // 如果可以恢复滚动位置，则恢复滚动位置
      return savedPosition
    } else {
      // 否则让页面回到顶部并设置滚动等待时间为 500ms
      return new Promise((resolve) => {
        setTimeout(() => {
          resolve({ left: 0, top: 0 })
        }, 10)
      })
    }
  }
});

// 全局守卫：
router.beforeEach(async (to, from, next) => {
  next();

})

export default router;
